package top.dvvj.wheelmall.common.util;

import com.github.pagehelper.PageInfo;
import lombok.Data;
import top.dvvj.wheelmall.common.enums.CodeEnum;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页封装对象
 *
 * @author Dvj
 * @date 2021/9/17 22:31
 */
@Data
public class PageResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 响应状态码
     */
    private Integer code;
    /**
     * 提示信息
     */
    private String msg;
    /**
     * 响应数据
     */
    private List<T> list;
    /**
     * 当前页
     */
    private Integer page;
    /**
     * 分页大小
     */
    private Integer pageSize;
    /**
     * 总页数
     */
    private Long total;

    private PageResult(List<T> list, Integer page, Integer pageSize, Long total) {
        this.code = CodeEnum.SUCCESS.getCode();
        this.msg = CodeEnum.SUCCESS.getMsg();
        this.list = list;
        this.page = page;
        this.pageSize = pageSize;
        this.total = total;
    }

    public static <T> PageResult<T> ok(PageInfo<T> page) {
        if (page != null) {
            return new PageResult<>(page.getList(), page.getPageNum(), page.getPageSize(), page.getTotal());
        } else {
            return new PageResult<>(new ArrayList<>(), 0, 1, 0L);
        }
    }

}
